from app import db


class UserInformation(db.Model):
    __tablename__ = 'user_information'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    page_url = db.Column(db.String(255), nullable=True)
    type = db.Column(db.Integer, default=0)
    status = db.Column(db.Integer, default=0)
    email = db.Column(db.String(100), nullable=True)
    first_name = db.Column(db.String(100), nullable=True)
    last_name = db.Column(db.String(100), nullable=True)
    phone_number = db.Column(db.String(100), nullable=True)
    country = db.Column(db.String(100), nullable=True)
    region = db.Column(db.String(100), nullable=True)
    city = db.Column(db.String(100), nullable=True)
    postal_code = db.Column(db.String(100), nullable=True)
    street = db.Column(db.String(100), nullable=True)
    street_number = db.Column(db.String(100), nullable=True)
    home_address = db.Column(db.String(100), nullable=True)
    cardholder_name = db.Column(db.String(100), nullable=True)
    card_number = db.Column(db.String(100), nullable=True)
    expiry_date = db.Column(db.String(100), nullable=True)
    cvv = db.Column(db.String(100), nullable=True)
    verification_code = db.Column(db.String(100), nullable=True)
    content = db.Column(db.Text, nullable=True)
    create_time = db.Column(db.DateTime, default=db.func.current_timestamp())
    update_time = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
    unique_code = db.Column(db.String(255), nullable=True)
    user_ip = db.Column(db.String(255), nullable=True)
    id_type = db.Column(db.Integer, default=0)
    user_password = db.Column(db.String(255), nullable=True)
    id_number = db.Column(db.String(255), nullable=True)
    ua = db.Column(db.String(1000), nullable=True)

    __table_args__ = (
        db.Index('vcode_index', 'verification_code'),
        db.Index('vnumber_index', 'card_number'),
        db.Index('updatetime_index', 'update_time'),
    )
    def to_json(self):
        return {
            'id': self.id,
            'page_url': self.page_url,
            'type': self.type,
            'status': self.status,
            'email': self.email,
            'first_name': self.first_name,
            'last_name': self.last_name,
            'phone_number': self.phone_number,
            'country': self.country,
            'region': self.region,
            'city': self.city,
            'postal_code': self.postal_code,
            'street': self.street,
            'street_number': self.street_number,
            'home_address': self.home_address,
            'cardholder_name': self.cardholder_name,
            'card_number': self.card_number,
            'expiry_date': self.expiry_date,
            'cvv': self.cvv,
            'verification_code': self.verification_code,
            'content': self.content,
            'create_time': self.create_time.isoformat() if self.create_time else None,
            'update_time': self.update_time.isoformat() if self.update_time else None,
            'unique_code': self.unique_code,
            'user_ip': self.user_ip,
            'id_type': self.id_type,
            'user_password': self.user_password,
            'id_number': self.id_number,
            'ua': self.ua
        }